import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-09-12
 * Time: 15:51
 */
//生产者消费模型
public class Demo2 {
    public static void main(String[] args) {
        BlockingDeque<Integer> queue = new LinkedBlockingDeque<>();

        //负责生产元素
        Thread s1 = new Thread(() -> {
            int count = 0;
            while (true) {
                try {
                    queue.put(count);
                    System.out.println("生产元素" + count);
                    count ++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        //负责消费元素
        Thread s2 = new Thread(() -> {
           while(true) {
               try {
                   int n = queue.take();
                   System.out.println("消费元素" + n);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });

        s1.start();
        s2.start();
    }
}
